package com.lc1993929.zookeeper;

import com.google.gson.Gson;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.List;

/**
 * Created by LiuChang on 2018/3/16.
 */
public class TestConnection {
    ZooKeeper zooKeeper;

    @Before
    public void init() throws IOException {
        zooKeeper = new ZooKeeper("hdp1:2181,hdp2:2181,hdp3:2181", 2000, null);
    }

    @After
    public void destroy() throws InterruptedException {
        zooKeeper.close();
    }

    /**
     * 创建数据节点
     */
    @Test
    public void createNode() throws KeeperException, InterruptedException {
        zooKeeper.create("/genius", "genius是天才".getBytes(Charset.forName("utf-8")), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }

    @Test
    public void testDeleteNode() throws KeeperException, InterruptedException {
        //-1表示删除这个节点的所有版本
        zooKeeper.delete("/genius", -1);
    }

    @Test
    public void testSetData() throws KeeperException, InterruptedException, UnsupportedEncodingException {
        zooKeeper.setData("/genius", "genius永远是天才".getBytes(Charset.forName("utf-8")), -1);
        byte[] data = zooKeeper.getData("/genius", false, null);
        System.out.println(new String(data, "utf-8"));
    }

    /**
     * 判断数据节点是否存在
     *
     * @throws KeeperException      the keeper exception
     * @throws InterruptedException the interrupted exception
     */
    @Test
    public void testExist() throws KeeperException, InterruptedException {
        Stat exists = zooKeeper.exists("/genius", false);
        System.out.println(exists == null ? "不存在" : "存在");
    }


    @Test
    public void testGetChildren() throws KeeperException, InterruptedException {
        List<String> childrens = zooKeeper.getChildren("/genius", null);
        //只返回子节点的名称，不带全路径
        childrens.forEach(System.out::println);
    }


    /**
     * zookeeper储存对象
     *
     * @throws KeeperException      the keeper exception
     * @throws InterruptedException the interrupted exception
     */
    @Test
    public void testPutObjectIntoZookeeper() throws KeeperException, InterruptedException {
        Person person = new Person();
        person.setName("genius");

        Gson gson = new Gson();
        String json = gson.toJson(person);

        zooKeeper.create("/genius", json.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);


        byte[] data = zooKeeper.getData("/genius", false, null);
        String jsonResult = new String(data);

        Person pJson = gson.fromJson(jsonResult, Person.class);
        System.out.println(pJson.getName());
    }


    @Test
    public void testWatch() throws KeeperException, InterruptedException {
        //监听节点数据内容发生变化的事件
        byte[] data = zooKeeper.getData("/genius", event -> {
            System.out.println(event.getPath());
            System.out.println(event.getState());
            System.out.println(event.getType());
        }, null);
        Thread.sleep(Integer.MAX_VALUE);
    }

    public static void main(String[] args) throws Exception {
        ZooKeeper zooKeeper = new ZooKeeper("hdp1:2181,hdp2:2181,hdp3:2181", 2000, null);

        byte[] data = zooKeeper.getData("/aaa/bbb", false, null);

        System.out.println(new String(data));

        zooKeeper.close();
    }
}
